<?php


namespace app\admin\command;

use app\admin\model\recharge\Setting;
use app\admin\model\Store;
use app\admin\model\Transaction;
use app\admin\model\user\Card;
use app\admin\model\Reserve;
use app\admin\model\Room;
use app\common\library\Yunding;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;

class TransTransactionExtrainfo extends Command
{

    protected function configure()
    {
        $this->setName('trans_transaction_extrainfo')
            ->setDescription('把记录里extra_info的字段的值转变一下');
    }

    protected function execute(Input $input, Output $output)
    {
        @ini_set("memory_limit", '-1');
        $stime = microtime(true);

//        $transactions = Transaction::where('type', 1)->select();
//
//        $count = 0;
//        foreach ($transactions as $item) {
//            $reserve = Reserve::where('transaction_id', $item->id)->find();
//            if ($reserve) {
//                $item->extra_info = '{"reserve_id":'.$reserve->id.',"room_id":'.$reserve->room_id.',"selectdate":"'.Carbon::parse($reserve->starttime)->toDateString().'","start":"'.$reserve->starttime.'","end":"'.$reserve->endtime.'","price":'.$reserve->fee.',"couponid":0}';
//                $item->save();
//            }
//
//            $count++;
//            echo $count . PHP_EOL;
//        }

        $transactions = Transaction::where('type', 2)->select();

        $count = 0;
        foreach ($transactions as $item) {
            if (is_numeric($item->extra_info)) {
                $rechargeSetting = Setting::get($item->extra_info);
                if ($rechargeSetting) {
                    $item->extra_info = '{"recharge_id":'.$rechargeSetting->id.',"num":'.$rechargeSetting->num.',"extra":'.$rechargeSetting->extra.'}';
                    $item->save();
                }

                $count++;
                echo $count . PHP_EOL;
            }
        }

        $etime = microtime(true);//获取程序执行结束的时间
        $total = $etime - $stime;   //计算差值
        $output->writeln($total);
    }

}
